﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace YcdTools
{
    public partial class FormAtxPass : Form
    {
        private SqlConnection _sqlConn = null;
        public FormAtxPass()
        {
            InitializeComponent();
        }

        private void FormAtxPass_Load(object sender, EventArgs e)
        {
            _sqlConn = new SqlConnection("server=172.16.71.3;database=ysodtestdb;uid=sa;pwd=123");
        }

        private void TextBoxEnterSn_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string sn = this.textBoxEnterSn.Text.Trim();
                if (sn.Length == 12)
                {
                    if (_sqlConn.State == ConnectionState.Closed)
                    {
                        _sqlConn.Open();
                    }

                    SqlCommand cmd = new SqlCommand($"SELECT SN,Po  FROM TestDataTable  WHERE DeviceType='PWT-39100301' AND SN='{sn}'", _sqlConn)
                    {
                        CommandType = CommandType.Text
                    };

                    double po;

                    using (var reader = cmd.ExecuteReader())
                    {
                        reader.Read();
                        po = double.Parse(reader[1].ToString());
                    }

                    cmd.Dispose();
                    _sqlConn.Close();
                    //写信息
                    this.textBoxRecvSn.Text = sn;
                    this.textBoxRecvPo.Text = po.ToString();

                    //判断
                    if (po > 3) {
                        this.labelResult.ForeColor = Color.Red;
                        this.labelResult.Text = "FAIL";
                    }
                    else
                    {
                        this.labelResult.ForeColor = Color.Green;
                        this.labelResult.Text = "PASS";
                    }

                    this.textBoxEnterSn.Clear();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
